﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TZUTEI
    {

        public TZUTEI()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VZUTEI();
            DataContext = MV;
            InitializeComponent();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            if (!IsAuto)
                return;
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return; 
            dwData = TBMain.NewRow();
            dwData["ZUMANDANT"] = app.Mandant;
            dwData["ZUAUFKRS"] = EXAUF.Substring(0, 2);
            dwData["ZUAUFLFD"] = EXAUF.Substring(2, 6);
            dwData["ZUAUFPOS"] = EXAUF.Substring(8, 3);
            SResult =TMV.QZU41(dwData);
            MergeData(SResult,false);
            base.OnEXAUFChg(vlaue);
        }
        public override void OnAutoChg(bool value)
        {
            if (!IsAuto)
                return;
            OnEXAUFChg(EXAUF);
            base.OnAutoChg(value);
        }
        bool IsRepair = false;
        private void btnInitializedMU_Click(object sender, RoutedEventArgs e)
        {
            txtMessage.Visibility = Visibility.Visible;
            if (dwAB == null)
            {
                MessageBox.Show("请选择需要计划的生产单");
                return;
            }
            DataSet SResult;

            //正对所有的单创建一个记录集合，针对每个单的记录集合，创建生产单
            try
            {
                dwData = TMV.GD.TBMain.NewRow();
                dwData["GAMANDANT"] = app.Mandant;
                dwData["GAAUFKRS"] = dwAB["ABPAUFKRS"];
                dwData["GAAUFLFD"] = dwAB["ABPAUFLFD"];
                dwData["GAAUFPOS"] = dwAB["ABPAUFPOS"];
                dwData["GASA"] = 713;
                SResult = TMV.GD.QGD29(dwData);
                TMV.SetProd.Merge(SResult);
                dwData = TMV.ABK.TBMain.NewRow();
                dwData["ABKMANDANT"] = app.Mandant;
                dwData["ABKAUFKRS"] = dwAB["ABPAUFKRS"];
                dwData["ABKAUFLFD"] = dwAB["ABPAUFLFD"];  
                SResult = TMV.ABK.QABK01(dwData);
                TMV.SetProd.Merge(SResult);
                TBTemp = TMV.ABP.TBMain.Clone();
                TBTemp.TableName = "ABPOS";
                TMV.SetProd.Merge(TBTemp);
                /* dwData = TMV.VABA.TBMain.NewRow();
                 dwData["ABAMANDANT"] = app.Mandant;
                 dwData["ABAAUFKRS"] = dwAB["ABPAUFKRS"];
                 dwData["ABAAUFLFD"] = dwAB["ABPAUFLFD"];
                 dwData["ABAAUFPOS"] = dwAB["ABPAUFPOS"];
                 SResult = TMV.VABA.QueryICC_ABAX(dwData,5);
                 TMV.SetProd.Merge(SResult);   */
                TBTemp = TMV.VABA.TBMain.Copy();
                TBTemp.TableName = "ICC_ABAX";
                TMV.SetProd.Merge(TBTemp);

                TBTemp = TMV.TBMain.Copy();
                TBTemp.TableName = "ZUTEI_ITX";
                TMV.SetProd.Merge(TBTemp);
                SResult = TMV.PrePlan(TMV.SetProd, dwAB, IsRepair);

                string path = @"C:\IntexReport\Data\YGRMU";
                SResult.Tables["ABPOS"].TableName = "ABPOS_EXT";
                TMV.ABP.Save2Xml(SResult, path);
                TMV.ABP.AutoConvert(path, "YGRMUZT");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("遇到问题:" + ex.Message + "终止", StatusEventType.StopProgress);
            }
            finally
            {

                if (IsRepair)
                {
                    MessageBox.Show("请记住分盘单号码，并对它进行分盘。");
                }
            }
        }
    }
}
